//
//  StoreCommissionDetailVc.m
//  WGFGlobalHouses
//
//  Created by waiguofang on 15/6/9.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "StoreCommissionDetailVc.h"
#import "BdTV.h"
#import "config.h"
#import "DealerDataManage.h"
#import "MJRefresh.h"
#import "StoreCommissionBrokerCell.h"
#import "CommissionHeadCell.h"
#import "BrokerCommissionVc.h"
#import "TableMenu.h"
#import "BrokerQueryVc.h"
#import "PeriodicQueryVc.h"
#import "CommissionSubVc.h"
#import "TxtReadVc.h"





@interface StoreCommissionDetailVc ()<TableViewAction,DaySelectDelegate,CommissionHeadCellDelegate>
@property (nonatomic,strong)NSArray *dataArray;
@property(nonatomic,assign) int pageIndex;
@property (strong, nonatomic) DealerDataManage *dm;

@property (strong, nonatomic) TableMenu *menu;
@property (strong, nonatomic) NSArray *menuArray;

@end

@implementation StoreCommissionDetailVc
@synthesize dataArray,bufData,pageIndex,dm,menu,menuArray;



- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initView];
    
    
    [self refreshData];
    
}




-(void)initView{
    [super initView];
    
    BdTV *title = [[BdTV alloc]initWithFrame:CGRectMake(0, 0, PMW, 44)];
    [title.backBtn setTag:11];
    [title.backBtn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];
    [[title doneBtn] setImage:[UIImage imageNamed:@"icon27"] forState:UIControlStateNormal];
    [[title doneBtn]setTag:12];
    [[title doneBtn]addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];
    [title setMianTitle:bufData.storeName];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:title];
    
    [self.tableView addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(refreshData)];
    [self.tableView addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(loadMore)];

    
    dm = [DealerDataManage new];
    
    menu = [TableMenu new];
    menu.myTableViewDelegate = self;
    
}


-(void)refreshData{
    [super refreshData];
    
    pageIndex=1;
    
    [self.nodataCell setState:nodataCellStateLoading];
    
     __weak typeof(self) baseSelf = self;
    
    [dm refreshStoreCommissionDetailData:pageIndex storeId:bufData.storeCode CallBack:^(ResulteMod *resultMod) {
        
         [baseSelf.tableView.header endRefreshing];
        
        if (resultMod.resltState==ResultNetError) {
            
            [baseSelf.nodataCell setState:nodataCellStateNetError];
        }else if (resultMod.resltState==ResultFail){
            [baseSelf.nodataCell setCodeErrorTip:resultMod.resultCode];
        }else{
            
            baseSelf.dataArray = resultMod.dataMod;
            
            if(baseSelf.dataArray.count>0){
                baseSelf.pageIndex++;
            }else{
                [baseSelf.nodataCell setState:nodataCellStateNodata];
            }
        }
        
        [baseSelf.tableView reloadData];
        
    }];
}



-(void)loadMore{

    __weak typeof(self) baseSelf = self;
    
    [dm refreshStoreCommissionDetailData:pageIndex storeId:bufData.storeCode CallBack:^(ResulteMod *resultMod) {
        
        [baseSelf.tableView.footer endRefreshing];
        
        
        if (resultMod.resltState==ResultNetError) {
            
            [baseSelf.nodataCell setState:nodataCellStateNetError];
        }else if (resultMod.resltState==ResultFail){
            [baseSelf.nodataCell setCodeErrorTip:resultMod.resultCode];
        }else{
            
            NSArray *data = resultMod.dataMod;
            
            if(data.count>0){
                baseSelf.pageIndex++;
            }
            
            NSMutableArray *bufDataArray = [[NSMutableArray  alloc]init];
            [bufDataArray addObjectsFromArray:baseSelf.dataArray];
            [bufDataArray addObjectsFromArray:data];
            baseSelf.dataArray = [[NSArray alloc]initWithArray:bufDataArray];
        }
        
        [baseSelf.tableView reloadData];
        
    }];


}




-(void)btnOnClick:(UIButton *)sender{
    
    if (sender.tag == 11) {
        [self.navigationController popViewControllerAnimated:YES];
    }else if (sender.tag==12){//查询
        
        if (menuArray.count==0) {
            
//            LaunchItem *item1 = [[LaunchItem alloc]init];
//            [item1 setCategoryId:1];
//            [item1 setName:@"周期查询"];
            
            
            LaunchItem *item3 = [[LaunchItem alloc]init];
            [item3 setCategoryId:3];
            [item3 setName:@"经纪人查询"];
            
            
            menuArray = [NSArray arrayWithObjects:item3, nil];
            [menu setData:menuArray];
        }
        
        [menu showInView:[[UIApplication sharedApplication] keyWindow] under:sender fromBar:YES];

        
        
    }else if (sender.tag==56){
        [self refreshData];
    }
}



#pragma mark tableView delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    if (dataArray&&dataArray.count>0) {
        return 2;
    }
    
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section==1) {
        return 40;
    }
    return 0;
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if (section==0) {
        return [UIView new];
    }
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, PMW, 40)];
    [view setBackgroundColor:UIColorFromRGB(0xf7f7f7)];
    UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, view.frame.size.height)];
    [titleLab setTextColor:UIColorFromRGB(0x999999)];
    [titleLab setFont:[UIFont systemFontOfSize:17]];
    [titleLab setText:@"经纪人"];
    [view addSubview:titleLab];
    
    
    {
        UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(view.frame.size.width-110, 0, 100, view.frame.size.height)];
        [titleLab setTextColor:UIColorFromRGB(0x999999)];
        [titleLab setFont:[UIFont systemFontOfSize:17]];
        [titleLab setTextAlignment:NSTextAlignmentRight];
        [titleLab setText:@"佣金"];
        [view addSubview:titleLab];
    }
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(view.frame)-1, CGRectGetWidth(view.frame), 1)];
    [line setBackgroundColor:[UIColor redColor]];
    [view addSubview:line];
    
    return view;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (dataArray.count==0) {
        return tableView.frame.size.height;
    }
    
    if (indexPath.section==0) {
      return [CommissionHeadCell getHight];
    }else{
        return [StoreCommissionBrokerCell getHight:nil withTableView:nil];
    }
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    if (dataArray&&dataArray.count>0) {
        [self.tableView setScrollEnabled:YES];
        if (section==0) {

            if (dataArray.count>1) {
                return 1;
            }else{
                return 0;
            }
            
        }else{
            return self.dataArray.count-1;
        }
    }else{
        [self.tableView setContentOffset:CGPointMake(0, 0)];
        [self.tableView setScrollEnabled:NO];
        return 1;
    }
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (dataArray.count==0) {
        return self.nodataCell;
    }
    
    if (indexPath.section==0) {
        
        CommissionHeadCell *cell = [CommissionHeadCell creatViewWithTableView:tableView];
        CommissionData *data = [dataArray firstObject];
        cell.delegate = self;
        [cell setData:data];
        return cell;
        
    }else{
        
        
        StoreCommissionBrokerCell *cell = [StoreCommissionBrokerCell cellWithTableView:tableView];
        
        StoreCommissionBroker *data = [dataArray objectAtIndex:indexPath.row+1];
        
        [cell setData:data];
        
        return cell;
        
    }
}






- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (dataArray.count>0) {
        
        if (indexPath.section==1) {
            
            StoreCommissionBroker *data = [dataArray objectAtIndex:indexPath.row+1];
            
            BrokerCommissionVc *vc = [[BrokerCommissionVc alloc]init];
            vc.bufBroker = data;
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
}


#pragma mark menu Delegate
-(void)tableViewItemDidSelected:(LaunchItem*)item senderBtnTag:(int)tag{
    
    switch (item.categoryId) {
            
        case 1://周期查询
        {
            PeriodicQueryVc *vc = [PeriodicQueryVc new];
            vc.delegate =self;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
            
            
        case 3://经纪人
        {
            [self.navigationController pushViewController:[BrokerQueryVc new] animated:YES];
            
        }
            break;
            
            
        default:
            break;
    }
    
}



#pragma mark QueryVc delegate
-(void)timeSelectedFrom:(BirthDay *)begin to:(BirthDay *)end{
    
    CommissionSubVc *vc = [[CommissionSubVc alloc]init];
    [vc queryDataByBirthDay:begin to:end];
    [self.navigationController pushViewController:vc animated:YES];
}



-(void)CommissionHeaderCellQuestionBtnOnClick{
    
    TxtReadVc *vc = [TxtReadVc new];
    vc.titleStr = @"佣金明细";
    vc.subCategoryTitleStr = @"佣金何时到账 ?";
    vc.txtStr = @"客户付完尾款，收房一到两个工作日佣金到账。";
    
    [self.navigationController pushViewController:vc animated:YES];
    
}





@end
